/*
 * @Description: 
 * @Author: Chenx
 * @Date: 2023-03-03 17:10:35
 * @LastEditors: Chenx
 * @LastEditTime: 2023-03-06 14:58:02
 */
import { createI18n } from 'vue-i18n'
import tc from './lang/tc'
import sc from './lang/sc'
import en from './lang/en'

const messages = {
  tc,
  sc,
  en
}
// const language = (navigator.language || 'tc').toLocaleLowerCase() // 这是获取浏览器的语言
// 获取本地语言
export const getLocale = ():string => {
  // 优先从local取语言
  const localLanguage = localStorage.getItem('language')
  if (localLanguage !== null && localLanguage !== undefined && localLanguage !== '') {
    document.documentElement.lang = localLanguage
    return localLanguage
  }

  // 从浏览器对象取语言
  const language = navigator.language.toLowerCase()
  const locales = Object.keys(messages)
  for (const locale of locales) {
    if (language.includes(locale)) {
      document.documentElement.lang = locale
      return locale
    }
  }

  // 默认中文 tc
  return 'tc'
}
const i18n = createI18n({
  legacy: false,
  locale: getLocale(), // 首先从缓存里拿，没有的话就用浏览器语言，
  fallbackLocale: 'tc', // 设置备用语言
  messages
})

export default i18n